package com.example.carrierapp.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.example.carrierapp.util.DisplayUtil;
import com.example.carrierapp.util.ToastUtils;
import com.example.lb.fingerdemo.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ModifyVolumeDialog extends Dialog {
    private Activity mActivity;

    @BindView(R.id.tv_modify_des)
    TextView mTvDes;
    @BindView(R.id.et_modify_volume)
    EditText mEtInput;
    @BindView(R.id.et_modify_carnum)
    TextView mEtCarNum;
    @BindView(R.id.tv_modify_volume)
    TextView mTvVolume;

    private int mId;
    private String mCarNum;
    private boolean mOnlyCarNum;//只能修改

    public ModifyVolumeDialog(@Nullable Activity context) {
        super(context, R.style.float_dialog);
        setContentView(R.layout.dialog_modify_volume);
        ButterKnife.bind(this);
        mActivity = context;
        initParameter();
        initView();
        setCanceledOnTouchOutside(true);

    }

    private void initView() {
    }

    @SuppressLint("SetTextI18n")
    public void showDialogNotVolume(String code, int id, String carNum) {
        mId = id;
        mCarNum = carNum;
        mOnlyCarNum = true;

        mEtInput.setVisibility(View.GONE);
        mTvVolume.setVisibility(View.GONE);

        mTvDes.setText("运单" + code + "在输入框内修改并提交即可完成修改！");
        mEtInput.setText("");
        mEtCarNum.setText(carNum);
        mEtInput.setEnabled(false);
        this.show();
    }

    @SuppressLint("SetTextI18n")
    public void showDialogAll(String code, int id, String carNum) {
        mId = id;
        mCarNum = carNum;
        mOnlyCarNum = false;
        mEtInput.setVisibility(View.VISIBLE);
        mTvVolume.setVisibility(View.VISIBLE);

        mTvDes.setText("运单" + code + "在输入框内修改并提交即可完成修改！");
        mEtInput.setText("");
        mEtCarNum.setText(carNum);
        this.show();
    }

    @OnClick({R.id.et_modify_carnum})
    public void onCarNumClick(View v) {
        if (mOnVolumeClickListeber != null) {
            mOnVolumeClickListeber.onCarNumClick();
        }
    }

    @OnClick({R.id.ib_modify_close})
    public void onClick(View v) {
        dismiss();
    }

    @OnClick(R.id.tv_modify_sure)
    void onSureClick() {
        if (!TextUtils.isEmpty(mEtInput.getText().toString().trim()) || !TextUtils.isEmpty(mEtCarNum.getText().toString().trim())) {
            if (mOnVolumeClickListeber != null) {
                String volume = mEtInput.getText().toString().trim();
                if (TextUtils.isEmpty(mEtInput.getText().toString().trim())) {
                    volume = "0";
                }
                double vo = Double.valueOf(volume);
                if (mEtInput.getVisibility() == View.VISIBLE && vo <= 0 || vo > 120) {
                    ToastUtils.showCenterToast(getContext(), "请输入0<签收量<=120吨！");
                    return;
                }
                mOnVolumeClickListeber.onVolume(volume, mEtCarNum.getText().toString().trim(), mId);
            }
        } else {
            ToastUtils.showCenterToast(getContext(), "未改变数据，不能点击！");
        }
    }

    private void initParameter() {
        Window window = getWindow(); // 得到对话框
        WindowManager.LayoutParams wl = window.getAttributes();
        WindowManager m = window.getWindowManager();
        Display d = m.getDefaultDisplay(); // 为获取屏幕宽、高
        wl.width = d.getWidth() - DisplayUtil.dip2px(getContext(), 50);
        wl.gravity = Gravity.CENTER;
        window.setAttributes(wl);
    }

    public void setCarNum(String carNum) {
        mEtCarNum.setText(carNum);
    }

    public interface OnVolumeClickListeber {
        void onVolume(String newVolume, String carNum, int id);

        void onCarNumClick();
    }

    private OnVolumeClickListeber mOnVolumeClickListeber;

    public void setOnVolumeClickListeber(OnVolumeClickListeber listeber) {
        mOnVolumeClickListeber = listeber;
    }
}
